package com.suishoupai.xmb.service;

import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Properties;

/**
 * Created by caojian on 2017/4/1.
 */
@Service
public class EmailService {
    @Value("${mail.host}")
    private String host;
    @Value("${mail.username}")
    private String username;
    @Value("${mail.password}")
    private String password;
    @Value("${mail.smtp.auth}")
    private boolean auth;
    @Value("${mail.smtp.starttls.enable}")
    private boolean starttlsEnable;
    @Value("${mail.timeout}")
    private long timeout;

    @Value("${template.template_update_delay}")
    private String templateUpdateDelay;
    @Value("${template.default_encoding}")
    private String defaultEncoding;
    @Value("${template.locale}")
    private String locale;

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        //加认证机制
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", auth);
        javaMailProperties.put("mail.smtp.starttls.enable", starttlsEnable);
        javaMailProperties.put("mail.smtp.timeout", timeout);
        mailSender.setJavaMailProperties(javaMailProperties);
        return mailSender;
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("classpath:mailTemplate");
        Properties properties = new Properties();
        properties.put("template_update_delay", templateUpdateDelay);
        properties.put("default_encoding", defaultEncoding);
        properties.put("locale", locale);
        freeMarkerConfigurer.setFreemarkerSettings(properties);
        return freeMarkerConfigurer;
    }

    /**
     * 发送邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param template html模版
     * @param model    参数
     * @return
     */
    public boolean sendMail(String to, String subject, String template, Map model) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,"utf-8");
        try {
            helper.setFrom(username);
            helper.setTo(to);
            helper.setSubject(subject);
            // 装载模板
            Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(template);
            // 加入map到模板中 输出对应变量
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, model);
            helper.setText(html, true);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        try {
            mailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
